/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bungabungaball.emitter;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.particles.Particle;
import org.newdawn.slick.particles.ParticleEmitter;
import org.newdawn.slick.particles.ParticleSystem;

/**
 * The BungaParticleEmitter supports the creation of visual effects.
 *
 * @author Ingo Klose, Stephan Meyer, Sebastian Lehmann
 * @version 1.0
 */
public class BungaParticleEmitter implements ParticleEmitter {

    /**
     * The rotation
     */
    private float rotation = 0;
    /**
     * The x coordinate of the center of the fire effect
     */
    private int x;
    /**
     * The y coordinate of the center of the fire effect
     */
    private int y;
    /**
     * The particle emission rate
     */
    private int interval = 1000;
    /**
     * Time til the next particle
     */
    private int timer;
    /**
     * The size of the initial particles
     */
    private float size = 10;
    /**
     * The radius of the ring
     */
    private int radius = 5;
    /**
     * Is the Emitting completed?
     */
    private boolean completed = false;
    /**
     * Number
     */
    private int anzahl = 0;

    /**
     * Constructor
     *
     * @param r
     * @param xGet
     * @param yGet
     */
    public BungaParticleEmitter(float r, float xGet, float yGet) {
        setRotation(r);
        x = (int) xGet + (int) (Math.sin(Math.toRadians((double) rotation)) * 35.0f) + 35;
        y = (int) yGet + (int) (Math.cos(Math.toRadians((double) rotation)) * 35.0f) + 35;
    }

    /**
     * Update the Emitter
     *
     * @param system
     * @param delta
     */
    @Override
    public void update(ParticleSystem system, int delta) {
        timer -= delta;
        for (int i = 0; i < 20; i++) {
            Particle p = system.getNewParticle(this, 1000);
            p.setColor(255, 0, 0, 1.f);
            p.setPosition(x, y);
            p.setSize(10);
            int test = 90;
            double factor = Math.random() * test - test / 2;
            p.setVelocity((float) Math.sin(Math.toRadians((double) rotation + factor)), (float) Math.cos(Math.toRadians((double) rotation + factor)), .1f);
        }
        if (timer < -100) {
            completed = true;
        }

    }

    /**
     * @return Return the variable completed
     */
    @Override
    public boolean completed() {
        return completed;
    }

    /**
     * WrapUp
     */
    @Override
    public void wrapUp() {
    }

    /**
     * resume
     */
    @Override
    public void resume() {
    }

    /**
     * Update the particle
     *
     * @param particle
     * @param delta
     */
    @Override
    public void updateParticle(Particle particle, int delta) {
        if (particle.getLife() > 1000.0f) {
            particle.kill();
        }


    }

    /**
     * Is the emitter enabled?
     *
     * @return Returns true
     */
    @Override
    public boolean isEnabled() {

        return true;
    }

    /**
     * Set the emitter enable
     *
     * @param enabled
     */
    @Override
    public void setEnabled(boolean enabled) {
    }

    /**
     *
     * @return Returns false
     */
    @Override
    public boolean useAdditive() {
        return false;
    }

    /**
     *
     * @return Returns null
     */
    @Override
    public Image getImage() {
        return null;
    }

    /**
     *
     * @return Returns false
     */
    @Override
    public boolean isOriented() {
        return false;
    }

    /**
     *
     * @param system
     * @return Returns false
     */
    @Override
    public boolean usePoints(ParticleSystem system) {
        return false;
    }

    /**
     *
     */
    @Override
    public void resetState() {
    }

    /**
     * Set the Rotation
     *
     * @param r
     */
    public void setRotation(float r) {
        rotation = -r+10;
    }

    /**
     * Get the Rotation
     *
     * @return rotation
     */
    public float getRotation() {
        return rotation;
    }
}
